using System.Collections.Generic;
using Newtonsoft.Json;
using TEngine;

namespace GameLogic
{
    public enum PLAYER_STATE
    {
        IDLE,
        SIT,
        READY,
        PLAYING,
        WATCH,
        OFFLINE,
        EXIT/** 已退出游戏 */,
        OUT/** 出局 */
    };
    public enum PLAYER_MATCH_STATE
    {
        /** 未参赛 */
        NOT_SIGN_UP,
        /** 首轮 */
        FIRST,
        /** 复赛 */
        SECOND,
        /** 决赛 */
        FINAL,
        /** 弃权 */
        QUIT,
        /** 淘汰 */
        OUT,
        /** 结束 */
        OVER
    }

    /// <summary>
    /// 用于游戏内玩家信息的存储
    /// </summary>
    public class PlayerVO : BindableVO
    {
        /// <summary>
        /// 从对象池租一个实例，需要归还，即调用Return()方法
        /// </summary>
        /// <returns></returns>
        public static PlayerVO Rent()
        {
            return MemoryPool.Acquire<PlayerVO>();
        }

        /// <summary>
        /// 归还对象到对象池
        /// </summary>
        public void Return()
        {
            MemoryPool.Release(this);
        }

        //除了在销毁和创建时，其他时候都使用User
        private UserVO _user;
        private UserVO User
        {
            get
            {
                if (_user == null)
                {
                    _user = MemoryPool.Acquire<UserVO>();
                }
                return _user;
            }
        }

        public int tableID = SocketDefinition.UNSPECIFIED_VALUE;

        public int numOfLost;
        public int numOfWin;
        public int numOfDraw;

        //等级
        private string _uLv = "";
        public string ULv
        {
            get => _uLv;
            set => SetProperty(ref _uLv, value);
        }

        private string _level = "0";
        public string Level
        {
            get => _level;
            set => SetProperty(ref _level, value);
        }

        private int _rank = 0;
        public int Rank
        {
            get => _rank;
            set => SetProperty(ref _rank, value);
        }

        private bool _isAI = false;
        public bool IsAI
        {
            get => _isAI;
            set => SetProperty(ref _isAI, value);
        }

        private PLAYER_STATE _state = PLAYER_STATE.SIT;
        public PLAYER_STATE State
        {
            get => _state;
            set => SetProperty(ref _state, value);
        }

        private bool _isBanker = false;
        public bool IsBanker
        {
            get => _isBanker;
            set => SetProperty(ref _isBanker, value);
        }

        protected int _seatID = SocketDefinition.UNSPECIFIED_VALUE;
        public int SeatID
        {
            get => _seatID;
            set
            {
                if (value != SocketDefinition.UNSPECIFIED_VALUE)
                {
                    SetProperty(ref _seatID, value);
                }
            }
        }

        private int _privateTableScore;
        public int PrivateTableScore
        {
            get => _privateTableScore;
            set => SetProperty(ref _privateTableScore, value);
        }

        private int _pocketMoney = 0;
        public int PocketMoney
        {
            get => _pocketMoney;
            set => SetProperty(ref _pocketMoney, value);
        }

        private bool _isOffline = false;
        public bool IsOffline
        {
            get => _isOffline;
            set => SetProperty(ref _isOffline, value);
        }

        private int _offlineTime = 0;
        public int OfflineTime
        {
            get => _offlineTime;
            set => SetProperty(ref _offlineTime, value);
        }

        public string UID
        {
            get => User.UserID;
            set => User.UserID = value;
        }

        public string UName
        {
            get => User.userName;
            set => User.userName = value;
        }

        public string UNickname
        {
            get => User.Nickname;
            set => User.Nickname = value;
        }

        public string Authentic
        {
            get => User.Authentic;
            set => User.Authentic = value;
        }

        public GENDER_TYPE userSex
        {
            get => User.userGender;
        }

        public string userSexString
        {
            set
            {
                if (string.IsNullOrEmpty(value)) return;
                User.userGender = value == "1" ? GENDER_TYPE.MALE : GENDER_TYPE.FEMALE;
            }
        }

        public string UHead
        {
            get => User.UHead;
            set => User.UHead = value;
        }

        public int UFace
        {
            get => User.UFace;
            set => User.UFace = value;
        }

        public DressInfo UDressInfo
        {
            get => User.DressInfo;
            set => User.DressInfoString = JsonConvert.SerializeObject(value);
        }

        public int Money
        {
            get => User.Money;
            set => User.Money = value;
        }

        public float RedPacketNum
        {
            get => User.RedPacketNum;
            set => User.RedPacketNum = value;
        }

        public bool IsOut
        {
            get => State == PLAYER_STATE.OUT;
        }

        public bool IsExit
        {
            get => State == PLAYER_STATE.EXIT;
        }

        public override void Clear()
        {
            base.Clear();

            tableID = SocketDefinition.UNSPECIFIED_VALUE;
            SeatID = SocketDefinition.UNSPECIFIED_VALUE;
            numOfLost = 0;
            numOfWin = 0;
            numOfDraw = 0;
            ULv = "";
            Level = "0";
            Rank = 0;
            IsAI = false;
            IsBanker = false;
            PrivateTableScore = 0;
            PocketMoney = 0;
            OfflineTime = 0;
            State = PLAYER_STATE.SIT;
            IsOffline = false;
            if (_user != null)
            {
                _user.Return();
                _user = null;
            }
        }

        public static readonly Dictionary<int, string> P_INFO_INT_KEYS = new Dictionary<int, string>
        {
            {1, nameof(tableID)},
            {3, nameof(State)},
            {4, nameof(Money)},
            {5, nameof(PrivateTableScore)},
            {7, nameof(numOfWin)},
            {8, nameof(numOfLost)},
            {9, nameof(numOfDraw)},
            {11, nameof(UFace)},
            {12, nameof(userSex)},
            {13, nameof(SeatID)},
            {15, nameof(Level)},
            {81, nameof(SeatID)},
            {0x10a, nameof(PocketMoney)}
        };

        public static readonly Dictionary<int, string> P_INFO_STRING_KEYS = new Dictionary<int, string>
        {
            {10, nameof(UName)},
            {14, nameof(ULv)},
            {26, nameof(UHead)},
            {37, nameof(UNickname)},
            {47, nameof(UNickname)},//（新版比赛服务器用）
            {500, nameof(UDressInfo)}
        };


    }
}